<?php

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;

class PopupLog extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'popup_log';


    public static function getLogs($where)
    {
        return self::select('*', $where);
    }

    public static function addLog($uid, $popupId, $isVisitor = false)
    {
        if (!$isVisitor) {
            $where = [
                'uid' => $uid,
                'popup_id' => $popupId
            ];
        } else {
            $where = [
                'vid' => $uid,
                'popup_id' => $popupId
            ];
        }

        if (self::has($where)) {
            $data['edittime'] = date('Y-m-d H:i:s');
            $ret = self::update($data, $where);
        } else {
            $data['popup_id'] = $popupId;
            $data['uid'] = $isVisitor ? 0 : $uid;
            $data['vid'] = $isVisitor ? $uid : '';
            $data['addtime'] = date('Y-m-d H:i:s');
            $ret = self::insert($data);
        }
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function updateLog($where, $data)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }
}